function inventory()
{
	var obj = {};
	obj.url = "index.php?option=com_ketnoi&view=inventory&tmpl=raw";
	var callback = function(){
		if($("#grid_inventory").length > 0)
		{
			var dataSource = $("#grid_inventory").data("kendoGrid").dataSource;
			dataSource.read();
		}	
	}
	obj.callback = callback;
	return obj;
}

function showModalCreateInventory_Grid(isUpdate)
{
	isUpdate = isUpdate || false;
	var obj = {};
	var link = "index.php?option=com_ketnoi&view=inventory&layout=create&tmpl=raw&quickcreate=1";
	obj.link = link;
	var callback = function(){
		if($("#form_create_inventory").length > 0)
		{
			var callbackCreate = function(response){
				var dataSource = $("#grid_inventory").data("kendoGrid").dataSource;
				if(!isUpdate)
					dataSource.page(1);	
				else dataSource.read();
				$('#form_create_inventory').closest("div.modal").find("div.modal-header button.close").trigger("click");
			}
			initControlinventory();	
			initCreateinventory(callbackCreate);
			
		}
		
	}
	obj.callback = callback;
	showModalLink(obj);
}

function showModalEditInventory_Grid(id)
{
	var obj = {};
	var link = "index.php?option=com_ketnoi&view=inventory&layout=create&tmpl=raw&quickcreate=1&id="+id;
	obj.link = link;
	var callback = function(){
		if($("#form_create_inventory").length > 0)
		{
			var callbackCreate = function(response){
				var dataSource = $("#grid_inventory").data("kendoGrid").dataSource;
				dataSource.read();
				$('#form_create_inventory').closest("div.modal").find("div.modal-header button.close").trigger("click");
			}
			initControlinventory();	
			initCreateinventory(callbackCreate);
			
		}
		
	}
	obj.callback = callback;
	showModalLink(obj);
}

function createhopdong()
{
	var grid = $("#grid_inventory").data("kendoGrid");
	var row = grid.select();
	var data = grid.dataItem(row);
	if(typeof(data) == "undefined")
	{
		alertify.alert("Vui lòng chọn máy cần tạo hợp đồng");
	}else
	{
		
	}	
		
}

function initControlinventory()
{
	$("#machine").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã máy</th><th class="k-header" >Tên máy</th></tr></thead></table></div></div>'
	});
	
	$("#machine").change(function(){
		var value = this.value;
		if(value == "")
		{
			$(this).next().find(".chzn-choices li.search-field").show();
		}else
		{
			$(this).next().find(".chzn-choices li.search-field").hide();
		}	
	})
	
	$("#nhacungcap").chosentable({
		max_selected_options:1,
		search_contains: true,
		no_results_text: 'Không tìm thấy:',
		template: function (text, templateData) {
	    	return [
	      		"<div class='k-grid-content'><table><tbody><tr><td>" + templateData.ma + "</td>",
	      		"<td>" + templateData.ten + "</td></tr></tbody></table>",      
	    	].join("");
	  	},
	  	headerTemplate:'<div class="k-grid-header"><div class="k-grid-header-wrap"> <table style="width:100%"><thead><tr><th class="k-header" style="width:50%">Mã nhà cung cấp</th><th class="k-header" >Tên nhà cung cấp</th></tr></thead></table></div></div>'
	});
	
	$("#nhacungcap").change(function(){
		var value = this.value;
		if(value == "")
		{
			$(this).next().find(".chzn-choices li.search-field").show();
		}else
		{
			$(this).next().find(".chzn-choices li.search-field").hide();
		}
	})
}

function initCreateinventory(callbackCreate)
{
	
	$("#form_create_inventory").validate({  
		errorPlacement: function(error, element) {
			if($(element).hasClass("error"))
			{
				hideLoader($('#form_create_inventory'));
			}
            error.remove();          
        },
        onkeyup:false,
        onfocusout:false,
        rules:{
            serial:{
                required:true,
                remote:{
        			url:"index.php?option=com_ketnoi&task=inventory.checkSerial&tmpl=raw",
        			data:{
        				serial:function(value){       	
        					return $("#form_create_inventory input[name='serial']").val();
        				}
        			},
        			beforeSend:function(){
        				showLoader($('#form_create_inventory'));
        			}
        		}
            },       
            machine_id:{
            	required:true
            }
            
        },
        messages:{
        	serial:{
                required:'Vui lòng nhập serial', 
                remote:'Máy photocopy với số serial này đã tồn tại'
            },    
            machine_id:{
            	required:"Vui lòng chọn máy"
            }
        }
    })
    
    $('#form_create_inventory').ajaxForm({
    	beforeSubmit:function(){   		
    		showLoader($('#form_create_inventory'));
    	},
    	success:function(response){
    		var obj = checkJSON(response);
    		if(obj.is_json)
    		{
    			if(!obj.result.success)
        		{
        			var error = obj.result.error;
        			alertify.alert(error);
        		}else
        		{
        			callbackCreate(obj.result);
        		}	
    		}
    		else
    		{
    			//var container = getMainContent();
    			//container.html(response);
    			//disableHashchange();   			
    			window.location.hash = "inventory";
    			//initControl("#tab_create_inventory");
    			//initCreateinventory();
    			//enableHashchange();
    				
    		}
    		hideLoader($('#form_create_inventory'));
    	}
    });
}

function deleteInventory(id)
{
	alertify.confirm("Bạn muốn xóa máy này?", function (e) {
	    if (e) {
	    	showLoader();
			var link = "index.php?option=com_ketnoi&view=inventory&task=inventory.delete&tmpl=raw&id="+id;
			$.get(link,"",function(response){
				var dataSource = $("#grid_inventory").data("kendoGrid").dataSource;
				dataSource.read();
				hideLoader();		
				alertify.set({ delay: 4000 });
				alertify.success("Xóa thành công");
			})
	    } 
	});
}




